<template>
  <div class="storeUserModify common-container">
    <div class="common-top">
      <span class="left">
        <span class="go-back" @click.stop="$router.go(-1)">返回</span>
      </span>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
    <div class="common-bot">
      <div class="modify-bot">
        <el-form :model="form" label-width="100px">
          <el-form-item required label="封面图片" style="width: 50%">
            <el-image :size="'large'" :src="form.cover"></el-image>
            <UploadFile
              :fileExName="['jpg', 'png', 'gif', 'jpeg']"
              @uploadSuc="uploadSuc"
            />
          </el-form-item>
          <el-form-item required label="标题" style="width: 50%">
            <el-input type="textarea" v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item required label="资讯内容" style="width: 50%">
            <el-input type="textarea" v-model="form.content"></el-input>
          </el-form-item>
          <el-form-item required label="资讯类型" style="width: 30%">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option
                v-for="item in newsTypeOptions"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item required label="标签" style="width: 50%">
            <div style="display: flex">
              <el-input
                v-model="tempTags"
                style="width: calc(100% - 80px)"
              ></el-input>
              <el-button
                type="primary"
                style="margin-left: 10px"
                @click.stop="addTags"
                >添加</el-button
              >
            </div>
            <div style="padding-top: 10px">
              <el-tag
                style="margin-left: 5px"
                @close="delTags(tag, index)"
                v-for="(tag, index) in form.tags"
                :key="index"
                closable
              >
                {{ tag }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="排序" style="width: 30%">
            <el-input v-model="form.queue"></el-input>
          </el-form-item>
          <el-form-item label="推荐" style="width: 30%">
            <el-switch v-model="form.top"></el-switch>
          </el-form-item>
          <el-form-item label="是否启用" style="width: 30%">
            <el-switch v-model="form.enable"></el-switch>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import UploadFile from "@/components/uploadFile";
import {
  provinceAndCityData,
  CodeToText,
  TextToCode,
} from "element-china-area-data";
import ManaApi from "@/api/index";
export default {
  name: "storeUserModify",
  components: { UploadFile },
  data() {
    return {
      tempTags: "",
      areaSelectData: provinceAndCityData,
      genderOptions: [
        {
          label: "男",
          value: "male",
        },
        {
          label: "女",
          value: "female",
        },
      ],
      newsTypeOptions: [],
      vipOptions: [
        {
          label: "普通版",
          value: "0",
        },
      ],
      form: {
        content: "",
        cover: "",
        enable: true,
        formId: 0,
        queue: 0,
        tags: [],
        title: "",
        top: true,
        type: "normal",
      },
      roleOptions: [
        {
          label: "老板",
          value: "boss",
        },
        {
          label: "员工",
          value: "employee",
        },
      ],
    };
  },
  methods: {
    delTags(tag, index) {
      let tags = JSON.parse(JSON.stringify(this.form.tags));
      tags.splice(index, 1);
      this.$set(this.form, "tags", tags);
    },
    addTags() {
      if (!String(this.tempTags).trim()) {
        this.$message.error("请输入内容");
        return;
      }
      let tags = JSON.parse(JSON.stringify(this.form.tags));
      tags.push(this.tempTags);
      this.$set(this.form, "tags", tags);
      this.tempTags = "";
    },
    uploadSuc(data) {
      this.$set(this.form, "cover", data.url);
      console.log(data);
    },
    save() {
      let form = JSON.parse(JSON.stringify(this.form));
      ManaApi.news.edit(form).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.$router.go(-1);
        }
      });
    },
    getInfo() {
      ManaApi.news
        .detail({
          targetId: this.$route.query.id,
        })
        .then((res) => {
          this.form = {
            content: res.data.content,
            cover: res.data.cover,
            enable: res.data.enable,
            formId: res.data.id,
            queue: res.data.queue,
            tags: res.data.tags,
            title: res.data.title,
            top: res.data.top,
            type: res.data.type,
          };
        });
    },
    getConstant() {
      return new Promise((resolve, reject) => {
        ManaApi.constant
          .all({})
          .then((res) => {
            this.newsTypeOptions = res.data.NewsType;
            resolve();
          })
          .catch((err) => {
            reject();
          });
      });
    },
  },
  async mounted() {
    await this.getConstant();
    if (this.$route.query.type == "edit") {
      this.getInfo();
    }
  },
};
</script>
<style lang="scss" scoped>
.storeUserModify {
}
</style>